package com.ykdl.member.kid.prefs;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
/**
 * 
 * 升级信息 的记录
 * @since 1.0
 */
public class UpdatePrefs {
   public static final String LABEL = "update_version";
   private static final String LAST_CHECK="last_checktime";
   private static final String UPDATE_URL="update_url";
   private static final String NEW_VERSION="new_verson";
   private static final String DESCRIBE="describe";
   private static final String SET_TIP="set_tip";
   private static final String HOME_TIPTIME="home_tiptime";
   private static final String TONGJI="tongji";
   private static final String PUSH_VER="push_ver";
   private static final String UPDATE_FORCE="update_force";
   private static final String INSTALL_TYPE="installtype";
  
   //每天进入程序check一次
   public final static  long UPDATETIME=1000*60*60*24;
   //七天提示一次
   public final static  long UPDATE_HOMETIPTIME=1000*60*60*24*7;//强制升级，修改为0，正常的是7天
   
   
   public static void setPushVersion(Context context,float ver){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();
	   editor.putFloat(PUSH_VER, ver);
	   editor.commit();
   }
   
   public static float getPushVersion(Context context){
	   return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getFloat(PUSH_VER, 0);
   }
   
   public static long getTongJiTime(Context context){
		return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getLong(TONGJI, 0);	  
	   }
   public static void setTongJiTime(Context context,long time){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();
	   editor.putLong(TONGJI, time);
	   editor.commit();
	   }
   public static long getLastUpdateTime(Context context){
	return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getLong(LAST_CHECK, 0);	  
   }
   
   public static String getUpdateUrl(Context context){
		return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getString(UPDATE_URL, "");	  
	   }
   
   public static String getDescribe(Context context){
 		return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getString(DESCRIBE, "");	  
 	   }
   
   public static void setLastUpdateInfo(Context context,String upurl,String version,String desciption){
	   
	   String lastver= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getString(NEW_VERSION, "");
	   if(lastver!=null&&lastver.length()>0){
		   int newv=Integer.parseInt(version);
		   int oldver=Integer.parseInt(lastver);		   
		   if(newv>oldver){
				UpdatePrefs.setSetTip(context, false);
		   }		   
	   }
	   
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();
	   editor.putLong(LAST_CHECK, System.currentTimeMillis());
	   editor.putString(UPDATE_URL, upurl);
	   editor.putString(NEW_VERSION, version);	   
	   editor.putString(DESCRIBE, desciption);		 
	   editor.commit();
  } 
   
 public static void putLastUpdateTime(Context context){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();
	   editor.putLong(LAST_CHECK, System.currentTimeMillis());	 	 
	   editor.commit();
  } 
   
   
   
   
   public static void setSetTip(Context context,boolean tiped){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();	 
	   editor.putBoolean(SET_TIP, tiped);
	   editor.commit();
  } 
   
   public static boolean getSetTip(Context context){
	   return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getBoolean(SET_TIP, false);	  
  } 
   
  
   
   private static boolean atHomeTipTime(Context context){
	  long lasttip= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getLong(HOME_TIPTIME, 0);
	  if(System.currentTimeMillis()-lasttip>UPDATE_HOMETIPTIME){
		  return true;
	  }
	  return false;
   }
   
   public static boolean isHomeNeedTip(Context context){
	   if(hasNewVerson(context)&&atHomeTipTime(context)||getForceUpdate(context)==2){
		   setHomeTipTime(context,System.currentTimeMillis());
		   return true;
	   }  
	   return false;
  } 
   public static void setHomeTipTime(Context context,long time){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();	 
	   editor.putLong(HOME_TIPTIME,time);
	   editor.commit();
  } 
   
   //如果成功的告知了服务器，就设置为当前版本号，如果没有，或者不等于当前版本号，就一直想服务器提交
   public static void setInstallType(Context context,String verson){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();	 
	   editor.putString(INSTALL_TYPE,verson);
	   editor.commit();	  
   } 
   
   public static String getInstallType(Context context){
	   return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getString(INSTALL_TYPE, ""); 	   
  } 
   
   
   
   
      
   public static boolean hasNewVerson(Context context){
	   String newverson= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getString(NEW_VERSION, "");
	   if(newverson==null||newverson.length()==0){
		   return false;
	   }else{
		   int vercode = 0;
			try {
				vercode = context.getPackageManager().getPackageInfo(
						context.getPackageName(), PackageManager.GET_CONFIGURATIONS).versionCode;
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int newversion=Integer.parseInt(newverson);
			if(newversion>vercode){			
				return true;
			}else{
				return false;
			}		  
	   }
  }  
   
   
   
   public static void setForceUpdate(Context context,float ver){
	   Editor editor= context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).edit();
	   editor.putFloat(UPDATE_FORCE, ver);
	   editor.commit();
   }
   
   public static float getForceUpdate(Context context){
	   return context.getSharedPreferences(LABEL, Context.MODE_PRIVATE).getFloat(UPDATE_FORCE, 1);
   }
}
